package org.jgs1904.work.service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.jgs1904.work.dao.MessageDao;
import org.jgs1904.work.dao.UserDao;
import org.jgs1904.work.entity.Message;
import org.jgs1904.work.entity.Users;

/**
 * 留言业务类
 * @author dyf
 * @date 2020年2月4日
 */
public class MessageService {

	private MessageDao messageDao = new MessageDao();
	private UserDao userDao = new UserDao();
	
	public List<Message> getMyMessage(Long id) throws SQLException {
		return messageDao.selectAllByTargetId(id);
	}

	public int sendMessage(Message message) throws SQLException {
		
		message.setCreate_time(new Date());
		
		Users targetUser = userDao.selectOne(Users.builder().username(message.getTarget_username()).build());
		
		int result = 0;
		if (targetUser != null) {
			message.setTarget_id(targetUser.getId());
			result = messageDao.insert(message);
		}
		
		return result;
	}

}
